const util = require('../../utils/util');

Component({
    /**
     * 组件的初始数据
     */
    data: {
        // 计划名称
        name: null,
        // 计划目标
        mubiao: null,
        // 计划每周艾灸日
        selectList: {
            0: true,
            1: false,
            2: true,
            3: false,
            4: true,
            5: false,
            6: true
        },
        showTimePopup: false,
        showStarWeekTime: false,
        showEndWeekTime: false,
        timeFieldValue: '08:00',
        // 计划周期-开始日期：时间戳
        starWeekTimeValue: new Date().getTime(),
        // 计划周期-开始日期：字符串
        starWeekTimeValueStr: null,
        // 计划周期-结束日期：时间戳
        endWeekTimeValue: new Date().getTime(),
        // 计划周期-结束日期：字符串
        endWeekTimeValueStr: null,
        timeFormatter(type, value) {
            if (type === 'month') {
                return `${value}月`;
            }
            if (type === 'day') {
                return `${value}日`;
            }
            return value;
        },
        // 周期开始最小时间
        weekMinDate: new Date().getTime(),
        // 方案一穴位
        fangan1xuewei: [],
        // 方案二穴位
        fanganerxuewei: [],
        fangan1tagcss: 'tag',
        fangan2tagcss: 'tag-c',
        selectfangan: '方案一'
    },
    lifetimes: {
        attached: function () {
            const test_defen_params = wx.getStorageSync('test_defen_params')
            const {
                type1,
                type2,
                type3,
                type4,
                type5,
                type6,
                type7,
                type8
            } = test_defen_params
            console.log('------调理方案-------', test_defen_params)
            // 计算计划的穴位
            const fangan1Base = ['百会穴', '安眠穴', '三阴交', '神门穴']
            const fangan2Base = ['神阙穴']
            let fangan1 = []
            let fangan2 = []
            if (type1 >= 60) {
                fangan1 = [...fangan1, '心俞穴', '胆俞穴']
                fangan2 = [...fangan2, '胆俞穴', '通里穴', '神门穴', '内关穴']
            }
            if (type2 >= 60) {
                fangan1 = [...fangan1, '心俞穴', '胆俞穴']
                fangan2 = [...fangan2, '胆俞穴', '通里穴', '神门穴', '内关穴']
            }
            if (type3 >= 60) {
                fangan1 = [...fangan1, '行间穴', '侠溪穴', '风池穴', '外关']
                fangan2 = [...fangan2, '肝俞穴', '太冲穴', '三阴交']
            }
            if (type4 >= 60) {
                // 不建议艾灸
                // fangan1 = [...fangan1, '心俞穴', '胆俞穴']
            }
            if (type5 >= 60) {
                fangan1 = [...fangan1, '足三里', '中腕穴', '丰隆穴']
                fangan2 = [...fangan2, '合谷穴', '百会穴', '上腕穴', '天枢穴']
            }
            if (type6 >= 60) {
                // 瘀血内阻证
                // fangan1 = [...fangan1, '心俞穴', '胆俞穴']
            }
            if (type7 >= 60) {
                fangan1 = [...fangan1, '足三里', '心俞穴', '脾俞穴']
                fangan2 = [...fangan2, '三里穴', '条口穴', '照海穴', '列缺穴']
            }
            if (type8 >= 60) {
                fangan1 = [...fangan1, '神阙穴', '心俞穴', '肾俞穴', '太溪穴']
                fangan2 = [...fangan2, '太冲穴', '交感神经双穴', '三阴交', '心俞穴']
            }
            fangan1 = [...fangan1Base, ...fangan1]
            fangan2 = [...fangan2Base, ...fangan2]
            console.log('穴位获取1：', fangan1, fangan2);
            // 去重
            fangan1 = [...new Set(fangan1)]
            fangan2 = [...new Set(fangan2)]
            console.log('穴位获取2：', fangan1, fangan2);
            this.setData({
                fangan1xuewei: fangan1,
                fangan2xuewei: fangan2,
            })
        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
            // console.log('在组件实例被从页面节点树移除时执行')
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        clickFangan1() {
            this.setData({
                fangan1tagcss: 'tag',
                fangan2tagcss: 'tag-c',
                selectfangan: '方案一'
            })
        },
        clickFangan2() {
            this.setData({
                fangan1tagcss: 'tag-c',
                fangan2tagcss: 'tag',
                selectfangan: '方案二'
            })
        },
        clickWeekItem(e) {
            const day = e.currentTarget.dataset.value; // 获取当前点击的是星期几（0-6）
            const key = "selectList[" + day + ']';
            const that = this;
            this.setData({
                [key]: !that.data.selectList[day]
            })
        },
        bindinputName(e) {
            const value = e.detail.value
            this.setData({
                name: value
            })
        },
        bindinputMubiao(e) {
            const value = e.detail.value
            this.setData({
                mubiao: value
            })
        },
        // 确定计划
        quedingjihua() {
            const data = this.data
            const userinfo = wx.getStorageSync('userinfo')
            const params = {
                id: new Date().getTime(),
                timestr:new Date().getTime(),
                jhmzajr: JSON.stringify(data.selectList),
                zhouqistar: data.starWeekTimeValue,
                zhouqiend: data.endWeekTimeValue,
                name: data.name,
                mubiao: data.mubiao,
                txtime: data.timeFieldValue,
                openid: userinfo.openid,
                nickname: userinfo.nickname,
                sex: userinfo.sex,
                sfwc: '未完成',
                fangan1: JSON.stringify(data.fangan1xuewei),
                fangan2: JSON.stringify(data.fangan2xuewei),
                selectfangan: data.selectfangan
            }
            if (!params.name) {
                wx.showToast({
                    title: '请输入计划名称',
                    icon: 'none'
                })
                return;
            }
            const namelength = params.name.length
            console.log('namelength:', namelength);
            if (namelength > 8) {
                wx.showToast({
                    title: '计划名称不能超过8个字',
                    icon: 'none'
                })
                return;
            }
            if (!params.mubiao) {
                wx.showToast({
                    title: '请输入计划目标',
                    icon: 'none'
                })
                return;
            }
            if (!params.zhouqistar) {
                wx.showToast({
                    title: '请选择计划周期开始日期',
                    icon: 'none'
                })
                return;
            }
            if (!params.zhouqiend) {
                wx.showToast({
                    title: '请选择计划周期结束日期',
                    icon: 'none'
                })
                return;
            }
            if (!params.txtime) {
                wx.showToast({
                    title: '请选择提醒时间',
                    icon: 'none'
                })
                return;
            }
            console.log('确定计划', params)
            wx.showToast({
                title: '添加成功',
            })
            wx.setStorageSync('zuiXinTestJiHua', params)
            const openid = wx.getStorageSync('openid')

            const jihualistData = wx.getStorageSync('jihualistData') || [];
            console.log('获取当前所有计划', jihualistData)

            wx.setStorageSync('jihualistData', [...jihualistData, params])
            wx.setStorageSync('isJinrijihuawancheng', false)
            const myset = setTimeout(() => {
                // 返回计划页
                wx.switchTab({
                    url: '/pages/moxibustion/moxibustion',
                })
                wx.hideLoading()
                clearTimeout(myset)
            }, 1500)
        },
        onClickTime() {
            this.setData({
                showTimePopup: true
            })
        },
        onClose() {
            this.setData({
                showTimePopup: false,
                showEndWeekTime: false,
                showStarWeekTime: false
            })
        },
        starStarWeekTime() {
            this.setData({
                showStarWeekTime: true
            })
        },
        endendWeekTime() {
            this.setData({
                showEndWeekTime: true
            })
        },
        /**
         * 周期开始日期
         * @param {*} e 
         */
        starDatetimePickerInput(e) {
            this.setData({
                starWeekTimeValue: e.detail,
                starWeekTimeValueStr: util.formatTime(e.detail)
            })
        },
        /**
         * 周期结束日期
         * @param {*} e 
         */
        endDatetimePickerInput(e) {
            this.setData({
                endWeekTimeValue: e.detail,
                endWeekTimeValueStr: util.formatTime(e.detail)
            })
        },
        /**
         * 提醒时间
         * @param {*} e 
         */
        datetimePickerInput(e) {
            this.setData({
                timeFieldValue: e.detail,
            })
        },
    }
})